package cn.northsea.controller;


import cn.northsea.pojo.*;
import cn.northsea.service.ProductService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("product")
public class ProductController {

    @Autowired
    private ProductService productService;

    //查看原料列表
    @RequestMapping("/findMaterialAll")
    public String findMaterialAll(@RequestParam(required = false, defaultValue = "1") Integer pageNum,
                                  @RequestParam(required = false, defaultValue = "3") Integer pageSize,
                                  Model model,
                                  @RequestParam(required = false, defaultValue = "") String likename,
                                  @RequestParam(required = false, defaultValue = "") String start,
                                  @RequestParam(required = false, defaultValue = "") String end) {
        PageHelper.startPage(pageNum, pageSize);
        List<MaterialVo> materialVoList = null;
        PageInfo<MaterialVo> materialVoPageInfo = null;
        materialVoList = productService.findMaterialAll(likename, start, end);
        materialVoPageInfo = new PageInfo<>(materialVoList);
        model.addAttribute("likename", likename);
        model.addAttribute("start", start);
        model.addAttribute("end", end);
        model.addAttribute("Page", materialVoPageInfo);
        return "materialList";
    }

    //查看产品列表
    @RequestMapping("/findProductAll")
    public String findProductAll(@RequestParam(required = false, defaultValue = "1") Integer pageNum,
                                 @RequestParam(required = false, defaultValue = "3") Integer pageSize,
                                 Model model,
                                 @RequestParam(required = false, defaultValue = "") String likename,
                                 @RequestParam(required = false, defaultValue = "") String start,
                                 @RequestParam(required = false, defaultValue = "") String end) {
        PageHelper.startPage(pageNum, pageSize);
        List<ProductVo> productVoList = null;
        PageInfo<ProductVo> productVoPageInfo = null;
        productVoList = productService.findProductAll(likename, start, end);
        productVoPageInfo = new PageInfo<>(productVoList);
        model.addAttribute("likename", likename);
        model.addAttribute("start", start);
        model.addAttribute("end", end);
        model.addAttribute("Page", productVoPageInfo);
        return "productList";
    }

    //查看生产计划列表
    @RequestMapping("/findProductPlanAll")
    public String findProductPlanAll(@RequestParam(required = false, defaultValue = "1") Integer pageNum,
                                     @RequestParam(required = false, defaultValue = "3") Integer pageSize,
                                     Model model,
                                     @RequestParam(required = false, defaultValue = "") String likename,
                                     @RequestParam(required = false, defaultValue = "") String start,
                                     @RequestParam(required = false, defaultValue = "") String end) {
        PageHelper.startPage(pageNum, pageSize);
        List<ProductPlanVo> ProductPlanVoList = null;
        PageInfo<ProductPlanVo> ProductPlanVoPageInfo = null;
        ProductPlanVoList = productService.findProductPlanAll(likename, start, end);
        ProductPlanVoPageInfo = new PageInfo<>(ProductPlanVoList);
        model.addAttribute("likename", likename);
        model.addAttribute("start", start);
        model.addAttribute("end", end);
        model.addAttribute("Page", ProductPlanVoPageInfo);
        return "productPlanList";
    }

    //查看产品所需原料
    @RequestMapping("/toFindMaterial")
    public String toFindMaterial(Integer productId, Integer quantity, Integer productPlanId, Integer status, Model model) {
        List<MaterialProductVo> materialProductVoList = productService.toFindMaterial(productId, productPlanId);
        model.addAttribute("Page", materialProductVoList);
        model.addAttribute("productId", productId);
        model.addAttribute("productPlanId", productPlanId);
        model.addAttribute("quantity", quantity);
        model.addAttribute("status", status);
        return "findMaterial";
    }

    //查看原料暂存列表
    @RequestMapping("/getMaterial")
    public String getMaterial(@RequestParam(required = false, defaultValue = "1") Integer pageNum,
                              @RequestParam(required = false, defaultValue = "3") Integer pageSize,
                              Model model,
                              @RequestParam(required = false, defaultValue = "") String likename,
                              @RequestParam(required = false, defaultValue = "") String start,
                              @RequestParam(required = false, defaultValue = "") String end) {
        PageHelper.startPage(pageNum, pageSize);
        List<GetMaterialVo> getMaterialVoList = null;
        PageInfo<GetMaterialVo> getMaterialVoPageInfo = null;
        getMaterialVoList = productService.getMaterial(likename, start, end);
        getMaterialVoPageInfo = new PageInfo<>(getMaterialVoList);
        model.addAttribute("likename", likename);
        model.addAttribute("start", start);
        model.addAttribute("end", end);
        model.addAttribute("Page", getMaterialVoPageInfo);
        return "getMaterialList";
    }

    //    原料
    @RequestMapping("/findMaterialCategoryAll")
    public String findMaterialCategoryAll(Model model) {
        List<MaterialCategory> materialCategoryList = productService.findMaterialCategoryAll();
        model.addAttribute("category", materialCategoryList);
        return "materialForm";
    }

    @RequestMapping("/checkMaterialName")
    @ResponseBody
    public String checkMaterialName(String materialName) {
        Material material = productService.check(materialName);
        if (material == null) {
            return "OK";
        } else {
            return "ERROR";
        }
    }

    @RequestMapping("/checkMaterialCategoryName")
    @ResponseBody
    public String checkMaterialCategoryName(String materialCategoryName) {
        Material material = productService.checkMaterialCategoryName(materialCategoryName);
        if (material == null) {
            return "OK";
        } else {
            return "ERROR";
        }
    }

    @RequestMapping("/addMaterial")
    public String addMaterial(Material material) {
        productService.addMaterial(material);
        return "redirect:/product/findMaterialAll.do";//重定向格式
    }

    @RequestMapping("/addMaterialCategory")
    public String addMaterialCategory(MaterialCategory materialCategory) {
        productService.addMaterialCategory(materialCategory);
        return "redirect:/product/findMaterialAll.do";//重定向格式
    }

    @RequestMapping("/delMaterial")
    public String delMaterial(Integer[] ids) {
        productService.delMaterial(ids);
        return "redirect:/product/findMaterialAll.do";
    }

    @RequestMapping("findMaterialByMid")
    public String findMaterialByMid(Integer materialId, Model model, Integer pageNum) {
        Material material = productService.findMaterialByMid(materialId);
        List<MaterialCategory> materialCategoryList = productService.findMaterialCategoryAll();
        model.addAttribute("material", material);
        model.addAttribute("category", materialCategoryList);
        model.addAttribute("pageNum", pageNum);
        return "materialFormUpdate";
    }

    @RequestMapping("/updateMaterial")
    public String updateMaterial(Material material,
                                 Integer pageNum,
                                 Model model) {

        productService.updateMaterial(material);
        model.addAttribute("pageNum", pageNum);
        return "redirect:/product/findMaterialAll.do";
    }


    //产品
    @RequestMapping("/findProductCategoryAll")
    public String findProductCategoryAll(Model model) {
        List<ProductCategory> productCategoryList = productService.findProductCategoryAll();
        model.addAttribute("category", productCategoryList);
        return "productForm";
    }

    @RequestMapping("/checkProductName")
    @ResponseBody
    public String checkProductName(String productName) {
        Product product = productService.checkProductName(productName);
        if (product == null) {
            return "OK";
        } else {
            return "ERROR";
        }
    }

    @RequestMapping("/checkProductCategoryName")
    @ResponseBody
    public String checkProductCategoryName(String productCategoryName) {
        Product product = productService.checkProductCategoryName(productCategoryName);
        if (product == null) {
            return "OK";
        } else {
            return "ERROR";
        }
    }

    @RequestMapping("/addProduct")
    public String addProduct(Product product) {
        productService.addProduct(product);
        return "redirect:/product/findProductAll.do";//重定向格式
    }

    @RequestMapping("/addProductCategory")
    public String addProductCategory(ProductCategory productCategory) {
        productService.addProductCategory(productCategory);
        return "redirect:/product/findProductAll.do";//重定向格式
    }

    @RequestMapping("/delProduct")
    public String delProduct(Integer[] ids) {
        productService.delProduct(ids);
        return "redirect:/product/findProductAll.do";
    }

    @RequestMapping("findProductByPid")
    public String findProductByPid(Integer productId, Model model, Integer pageNum) {
        Product product = productService.findProductByPid(productId);
        List<ProductCategory> productCategoryList = productService.findProductCategoryAll();
        model.addAttribute("product", product);
        model.addAttribute("category", productCategoryList);
        model.addAttribute("pageNum", pageNum);
        return "productFormUpdate";
    }

    @RequestMapping("/updateProduct")
    public String updateProduct(Product product,
                                Integer pageNum,
                                Model model) {

        productService.updateProduct(product);
        model.addAttribute("pageNum", pageNum);
        return "redirect:/product/findProductAll.do";
    }

    @RequestMapping("findProductPlanById")
    public String findProductPlanById(Integer productPlanId, Integer pageNum, Model model) {
        ProductPlan productPlan = productService.findProductPlanById(productPlanId);
        List<Product> productList = productService.findProductList();
        model.addAttribute("productPlan", productPlan);
        model.addAttribute("productList", productList);
        model.addAttribute("pageNum", pageNum);
        return "productPlanFormUpdate";
    }

    @RequestMapping("findProductList")
    public String findProductList(Model model) {
        List<Product> productList = productService.findProductList();
        model.addAttribute("product", productList);
        return "productPlanForm";
    }

    @RequestMapping("/addProductPlan")
    public String addProductPlan(ProductPlan productPlan) {
        productService.addProductPlan(productPlan);
        return "redirect:/product/findProductPlanAll.do";//重定向格式
    }

    @RequestMapping("/delProductPlan")
    public String delProductPlan(Integer productPlanId, Integer pageNum, Model model) {
        productService.delProductPlan(productPlanId);
        model.addAttribute("pageNum", pageNum);
        return "redirect:/product/findProductPlanAll.do";
    }

    @RequestMapping("/updateProductPlan")
    public String updateProductPlan(ProductPlan productPlan, Integer pageNum, Model model) {
        productService.updateProductPlan(productPlan);
        model.addAttribute("pageNum", pageNum);
        return "redirect:/product/findProductPlanAll.do";
    }

    @RequestMapping("/findMaterialList")
    public String findMaterialList(Integer productId, Integer productPlanId, Integer status, Model model) {
        List<Material> materialList = productService.findMaterialList();
        Product product = productService.findProductByPid(productId);
        model.addAttribute("productId", productId);
        model.addAttribute("materialList", materialList);
        model.addAttribute("product", product);
        model.addAttribute("productPlanId", productPlanId);
        model.addAttribute("status", status);
        return "productMaterialForm";
    }

    @RequestMapping("/addProductMaterial")
    public String addProductMaterial(MaterialProduct materialProduct, Model model) {
        productService.addProductMaterial(materialProduct);
        model.addAttribute("productId", materialProduct.getProductId());
        model.addAttribute("status", materialProduct.getStatus());
        model.addAttribute("productPlanId", materialProduct.getProductPlanId());
        return "redirect:/product/toFindMaterial.do";
    }

    @RequestMapping("/delProductMaterial")
    public String delProductMaterial(Integer[] ids, Integer productId, Integer productPlanId, Integer status, Model model) {
        productService.delProductMaterial(ids);
        model.addAttribute("productId", productId);
        model.addAttribute("productPlanId", productPlanId);
        model.addAttribute("status", status);
        return "redirect:/product/toFindMaterial.do";
    }

    @RequestMapping("/updateProductMaterial")
    public String updateProductMaterial(Integer materialProductId, Integer status, Model model) {
        MaterialProduct materialProduct = productService.findProductMaterialById(materialProductId);
        List<Material> materialList = productService.findMaterialList();
        model.addAttribute("materialProduct", materialProduct);
        model.addAttribute("materialList", materialList);
        model.addAttribute("status", status);
        return "findMaterialFormUpdate";
    }

    @RequestMapping("/toUpdateProductMaterial")
    public String toUpdateProductMaterial(MaterialProduct materialProduct, Integer status, Model model) {
        productService.toUpdateProductMaterial(materialProduct);
        model.addAttribute("productId", materialProduct.getProductId());
        model.addAttribute("status", status);
        return "redirect:/product/toFindMaterial.do";
    }

    @RequestMapping("/certainMaterial")
    public String certainMaterial(Integer getMaterialId, Integer pageNum, Model model) {
        productService.certainMaterial(getMaterialId);
        model.addAttribute("pageNum", pageNum);
        return "redirect:/product/getMaterial.do";
    }

    @RequestMapping("/certainProductMaterial")
    public String certainProductMaterial(MaterialProduct materialProduct, Model model) {
        productService.certainProductMaterial(materialProduct);
        model.addAttribute("productId", materialProduct.getProductId());
        model.addAttribute("productPlanId", materialProduct.getProductPlanId());
        model.addAttribute("status", materialProduct.getStatus());//生产计划列表状态
        return "redirect:/product/toFindMaterial.do";
    }

    @RequestMapping("/findGetMaterialQuantity")
    @ResponseBody
    public String findGetMaterialQuantity(GetMaterial getMaterial, Integer quantity) {
        String s = productService.findGetMaterialQuantity(getMaterial, quantity);
        return s;
    }

    @RequestMapping("/checkProduct")
    @ResponseBody
    public String checkProduct(Integer productId, Integer productPlanId) {
        String s = productService.checkProduct(productId, productPlanId);
        return s;
    }

    @RequestMapping("/toProduct")
    public String toProduct(Integer productPlanId) {
        productService.toProduct(productPlanId);
        return "redirect:/product/findProductPlanAll.do";
    }

    @RequestMapping("/ProductPlanOver")
    public String ProductPlanOver(Integer productPlanId, Integer pageNum, Model model) {
        ProductPlan productPlan = productService.ProductPlanOver(productPlanId);
        Product product = productService.findProductByPid(productPlan.getProductId());
        List<Warehouse> warehouse = productService.findWarehouseAll();
        model.addAttribute("pageNum", pageNum);
        model.addAttribute("productPlan", productPlan);
        model.addAttribute("product", product);
        model.addAttribute("warehouse", warehouse);
        return "InWarehouse";
    }

    @RequestMapping("/InWarehouse")
    public String InWarehouse(WarehouseIn warehouseIn, Integer pageNum, Model model) {
        productService.InWarehouse(warehouseIn);
        model.addAttribute("pageNum", pageNum);
        return "redirect:/product/findProductPlanAll.do";
    }

    @RequestMapping("/getPid")
    public String getPid(Integer productPlanId, Integer pageNum, Model model) {
        productService.getPid(productPlanId);
        model.addAttribute("pageNum", pageNum);
        return "redirect:/product/findProductPlanAll.do";
    }

    @RequestMapping("/addGetMaterial")
    public String addGetMaterial(Model model) {
        List<Material> materialList = productService.findMaterialList();
        model.addAttribute("materialList", materialList);
        return "getMaterialForm";
    }

    @RequestMapping("/toAddGetMaterial")
    public String toAddGetMaterial(GetMaterial getMaterial, Model model) {
        productService.toAddGetMaterial(getMaterial);
        return "redirect:/product/getMaterial.do";
    }

}
